import requestAPI from '../../utils/requestApi'
Page({
  data: {
    // 轮播图区域数据
    banners: [],
    // 热门主题数据
    hotThemes: [],
    // 热门景点数据
    hotScenic: [],
    page: 1,
    pageSize: 3,
    loadingMore: false,
    hasMore: true,
    searchBoxTop: 98, // 默认值，可根据实际情况调整
    // 资讯列表
    noticeList: [],
  },
  // 主题跳转

  // 轮播图跳转
  onSwiperChange() {},
  onBannerTap: function (e) {
    const type = e.currentTarget.dataset.type;
    const id = e.currentTarget.dataset.id;
    let url = '';
    if (type === 'active') {
      url = '/pages/active-detail/active-detail?id=' + id;
    } else if (type === 'food') {
      url = '/pages/foodDetail/foodDetail?id=' + id;
    }
    // wx.navigateTo({
    //   url: url
    // });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setSearchBoxPosition();
    this.setData({
      isSearchBoxVisible: true
    });
    this.loadBanners()
    this.loadNoticeList()
    this.loadHotScenic()
    this.loadHotThemes()
  },
  setSearchBoxPosition() {
    // 获取菜单按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // console.log(menuButtonInfo);
    if (menuButtonInfo) {
      // 设置搜索框的 top 值为菜单按钮底部的 y 坐标
      const searchBoxTop = menuButtonInfo.top + 44;
      this.setData({
        searchBoxTop,
      });
    }
  },

  // 轮播图数据获取
  loadBanners: function () {
    const that = this;
    requestAPI.get(`/257044400`)
      .then(data => {
        const list = data
        that.setData({
          banners: list,
        });
      })
      .catch(error => {
        console.error('轮播图数据获取失败:', error);
      });
  },
  // 资讯数据获取
  loadNoticeList: function () {
    const that = this;
    requestAPI.get(`/257053432`)
      .then(data => {
        const list = data
        that.setData({
          noticeList: list,
        });
      })
      .catch(error => {
        console.error('资讯数据获取失败:', error);
      });
  },
  // 热门主题数据获取
  loadHotThemes: function () {
    const that = this;
    const hotThemesRelativeUrl = '/257041246';
    requestAPI.get(hotThemesRelativeUrl)
      .then(data => {
        const list = data
        that.setData({
          hotThemes: list,
        });
      })
      .catch(error => {
        console.error('请求热门主题数据失败:', error);
      });
  },
  // 热门景点请求
  loadHotScenic: function () {
    const that = this;
    const hotScenicRelativeUrl = `/257039027?page=${this.data.page}&pageSize=${this.data.pageSize}`;
    that.setData({
      loadingMore: true
    }); // 显示加载效果
    requestAPI.get(hotScenicRelativeUrl)
      .then(data => {
        const list = data;
        const newList = that.data.hotScenic.concat(list);
        that.setData({
          hotScenic: newList,
          loadingMore: false, // 隐藏加载效果
          hasMore: data.length === that.data.pageSize, // 判断是否还有更多数据
        });
    }).catch(error => {
      console.error('请求热门景点数据失败:', error);
      that.setData({
        loadingMore: false
      }); // 隐藏加载效果
    });
  },
  // 加载更多热门景点数据
  loadMoreHotScenic: function () {
    if (this.data.loadingMore || !this.data.hasMore) return;
    this.setData({
      page: this.data.page + 1
    });
    this.loadHotScenic();
  },
  // 监听页面滚动到底部
  onReachBottom: function () {
    this.loadMoreHotScenic();
    wx.showToast({
      title: this.data.hasMore ? '加载更多数据' : '没有更多了~',
      icon: this.data.hasMore ? 'loading' : 'none',
      duration: this.data.hasMore ? 1000 : 500,
      mask: true
    })
  },
  onInput: function (e) {
    const value = e.detail.value;
    // 处理输入值
    console.log(value);
  },
  // navigateToItemsPage(){
  //   const itemId = this.data.hotThemes[2].id
  //   // console.log(this.data.hotThemes[1].itemId);
  //   console.log(itemId);
  //   wx.navigateTo({
  //     url: `/pages/itmes/itmes?id=${itemId}`,
  //   })
  // },

})